<?php
class Allnews_Model
{
	/**
	 * Holds instance of database connection
	 */
	private $db;
	private $news_number;
		
	public function __construct()
	{
		$this->db = new MysqlImproved_Driver;
	}
	
	/**
	 * Fetches article based on supplied name
	 * 
	 * @param string $author
	 * 
	 * @return array $article
	 */
	public function get_article()
	{		
		//connect to database
		$this->db->connect();
		$this->get_page_data();
		$article['page_title'] = $this->page_data['title'];
		$article['page_description'] = $this->page_data['description'];
		$article['page_keywords'] = $this->page_data['keywords'];
		//sanitize data
		$article['content'] = $this->get_all_news(); 
		
		return $article;
	}
	
	public function get_all_news(){
		$this->db->prepare(
				"
				SELECT
				`news_title`,
				`news_link`,
				`news_creation_date`,
				`news_description`,
				`news_image`
				FROM
				`news`
				ORDER
				BY
				`news_creation_date`
				DESC;
				"
				);
		$this->db->query();
		
		$all_news_content = "";
		$this->news_number = 1;
		while($all_news_data = $this->db->fetch('array')){
			$news_title = $all_news_data['news_title'];
			$news_link = $all_news_data['news_link'];
			$news_creation_date = date("Y-m-d", strtotime($all_news_data['news_creation_date']));
			$news_description = $all_news_data['news_description'];
			$news_image = $all_news_data['news_image'];
			if($this->news_number < 3){
				$all_news_content .= $this->first_class_news($news_title, $news_link, $news_creation_date, $news_description, $news_image);
			}
			else if(($this->news_number >= 3) && ($this->news_number < 5)){
				$all_news_content .= $this->second_class_news($news_title, $news_link, $news_creation_date, $news_description, $news_image);
			}
			else if(($this->news_number >= 5) && ($this->news_number < 7)){
				$all_news_content .= $this->third_class_news($news_title, $news_link, $news_creation_date, $news_description); 
			}
			else if(($this->news_number >= 7)){
				$all_news_content .= $this->fourth_class_news($news_title, $news_link, $news_creation_date, $news_description);
			}
			// secure
			else {
			}
			$this->news_number++;
			if($this->news_number > 10){
				$this->news_number = 1;
			}	
		}
		return $all_news_content;
		
	}
	
	public function first_class_news($news_title, $news_link, $news_creation_date, $news_description, $news_image){
		$news_item = "
			<!-- Begin News Item -->
					<div class=\"item has-more\">
						<a class=\"lightbox\" href=\"../images/news/".$news_image."\"><img class=\"item-img left\" src=\"/images/news/".$news_image."\" alt=\"".$news_title."\" /></a>
						<div class=\"item-info\">
							<h4 class=\"news-h\">".$news_title."</h4>
							<span class=\"left date\">".$news_creation_date."</span>
							
							<div class=\"news-parag clear\">
								".$news_description."
							</div>
							<a class=\"more-arr\" href=\"/latestnews/".$news_link.".html\"></a>
							
						</div>
					</div>
					<!-- End News Item -->
		";
		return $news_item;
	}
	
	public function second_class_news($news_title, $news_link, $news_creation_date, $news_description, $news_image){
		$news_item = "
						<!-- Begin News Item -->
						<div class=\"item\">
							<a class=\"lightbox\" href=\"../images/news/".$news_image."\"><img class=\"item-img left\" src=\"/images/news/".$news_image."\" alt=\"".$news_title."\" /></a>
							<div class=\"item-info\">
								<h4 class=\"news-h\">".$news_title."</h4>
								<span class=\"left date\">".$news_creation_date."</span>
			
								<div class=\"news-parag clear\">
									".$news_description."
								</div>
								<a class=\"more-arr\" href=\"/latestnews/".$news_link.".html\"></a>
			
							</div>
						</div>
						<!-- End News Item -->
		";
		return $news_item;
	}
	
	public function third_class_news($news_title, $news_link, $news_creation_date, $news_description){
		$news_item = "
			<!-- Begin News Item -->
					<div class=\"item has-more\">
						
						<div class=\"item-info\">
							<h4 class=\"news-h\">".$news_title."</h4>
							<span class=\"left date\">".$news_creation_date."</span>
							
								<div class=\"news-parag clear\">
									".$news_description."
								</div>
								<a class=\"more-arr\" href=\"/morennews/".$news_link.".html\"></a>
							
						</div>
					</div>
					<!-- End News Item -->
		";
		return $news_item;
	}
	
	public function fourth_class_news($news_title, $news_link, $news_creation_date, $news_description){
		$news_item = "
						<!-- Begin News Item -->
					<div class=\"item\">
						
						<div class=\"item-info\">
							<h4 class=\"news-h\">".$news_title."</h4>
							<span class=\"left date\">".$news_creation_date."</span>
							
							<div class=\"news-parag clear\">
								".$news_description."
							</div>
							<a class=\"more-arr\" href=\"/morenews/".$news_link.".html\"></a>
							
						</div>
					</div>
					<!-- News News Item -->
		";
		return $news_item;
	}
	
	public function get_page_data(){
		//prepare query
		$this->db->prepare
		(
				"
				SELECT
				`page`,
				`title`,
				`description`,
				`keywords`
				FROM
				`page_description`
				WHERE
				`page` = 'news'
				LIMIT
				1
				;
				"
		);
	
		//execute query
		$this->db->query();
	
		$this->page_data = $this->db->fetch('array');
		return $this->page_data;
	}
	
}